var e = getApp(), a = require("../../A4730AC3A71FB5DFC21562C4223CA4D1.js");

Page({
    data: {
        addressId: 0,
        province: "",
        city: "",
        county: "",
        username: "",
        telephone: "",
        address: "",
        area: [],
        isDefault: !1,
        isLoading: !1
    },
    onLoad: function(a) {
        if (a.addressId) {
            this.setData({
                addressId: 1 * a.addressId
            });
            var t = e.globalData.editAddressItem;
            null !== t && this.setData({
                province: t.address_province,
                city: t.address_city,
                county: t.address_area,
                username: t.address_consignee,
                telephone: t.address_telephone,
                address: t.address_details,
                isDefault: 1 * t.address_is_default == 1
            });
        }
    },
    handleAddress: function(e) {
        var a = e.detail.value;
        a.length && this.setData({
            area: a,
            province: a[0] || "",
            city: a[1] || "",
            county: a[2] || ""
        });
    },
    handleInput: function(e) {
        var a = {};
        a[e.currentTarget.dataset.type] = e.detail.value, this.setData(a);
    },
    switchChange: function(e) {
        this.setData({
            isDefault: e.detail.value
        });
    },
    submit: function() {
        var t = this;
        if (!this.data.isLoading) {
            var s = this.data.username.trim();
            if (s) if (s.length < 2) wx.showToast({
                title: "请输入长度大于等于2的收货人",
                icon: "none"
            }); else {
                var d = this.data.telephone.trim();
                if (/^1(3|4|5|6|7|8|9)\d{9}$/.test(d)) {
                    var i = this.data.province, n = this.data.city;
                    if (i && n) {
                        var o = this.data.county;
                        o || (o = n, this.setData({
                            county: o
                        }));
                        var r = this.data.address.trim();
                        if (r.length < 5) wx.showToast({
                            title: "请输入长度大于等于5的详细地址",
                            icon: "none"
                        }); else {
                            this.setData({
                                isLoading: !0
                            }), wx.showLoading();
                            var l = {
                                address_consignee: s,
                                address_telephone: d,
                                address_province: i,
                                address_city: n,
                                address_area: o,
                                address_details: r,
                                address_is_default: this.data.isDefault ? 1 : 0
                            }, c = "", h = this.data.addressId;
                            1 * h == 0 ? c = "/v2/address/create" : (c = "/v2/address/modify", l.address_id = h), 
                            a.post(c, l).then(function(a) {
                                wx.hideLoading(), t.setData({
                                    isLoading: !1
                                }), 1001 === a.data.code && (e.globalData.editAddressItem = null, wx.showToast({
                                    title: a.data.msg,
                                    icon: "success",
                                    complete: function() {
                                        wx.navigateBack({
                                            delta: 1
                                        });
                                    }
                                }));
                            }).catch(function() {
                                wx.hideLoading(), t.setData({
                                    isLoading: !1
                                });
                            });
                        }
                    } else wx.showToast({
                        title: "请选择所在地区",
                        icon: "none"
                    });
                } else wx.showToast({
                    title: "请输入正确的手机号码",
                    icon: "none"
                });
            } else wx.showToast({
                title: "请输入收货人",
                icon: "none"
            });
        }
    }
});